/**
 * 功能描述 
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var FITCharge = require("../store/FITCharge");
	var store =require("store");
	var activeUser = store.get("user");
	var Dialog = require("dialog");		
	var Form = require("form");

    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode:".J-grid",
//        		autoRender : false,
        		url:"api/nonmembergroup/query",
        		params:function(){
        			var subnav = widget.get("subnav");
        			var obj = {
        					"pkServicePoint" : subnav.getValue("servicePoint"),
        					"status" : "CheckIn",
        					"pledgeStatus" : subnav.getValue("pledgeStatus"),        					
                			"checkInDate" : subnav.getValue("checkInDate").start,
                			"checkInDateEnd" : subnav.getValue("checkInDate").end,                    		
        					fetchProperties:FITCharge.fetchProperties//"servicePoint,startDate,endDate,pledge,groupUnit,tourType,chargeDate,status,fstatus",//
        			};
        			return obj; 

        		},
        		model:{
        			columns:[{
        				name:"servicePoint.name",
        				label:"目的地",
        			},{
						 name:"contactName",
						 label:"预约人/入住人",
						 format:function(value, row){
							 if(!value){
	        						return row.signUps[0].signUpRegister.personalInfo.name;
	        					}else{
	        						return value;
	        					}
	        			}
        			},{
						 name:"contactPhone",
						 label:"联系电话",
						 format:function(value, row){
							 if(!value){
	        						var personal=row.signUps[0].signUpRegister.personalInfo;
	        						if(personal.phone !=null){
	        							return personal.phone;
	        						}else if(personal.mobilePhone !=null){
	        							return personal.mobilePhone
	        						}else{
	        							return "";
	        						}
	        					}else{
	        						return value;
	        					}
		        		}
        			},{
        				name:"checkInDate",
        				label:"开始日期",
        				format : "date",
        			},
        			{
        				name:"checkOutDate",
        				label:"结束日期",
        				format : "date",
        			},
        			{
        				name:"pledge",
        				label:"押金金额",
        				format:function(val){
							return Number(val).toFixed(2);
						}
        			},
        			{
        				name:"groupUnit",
        				label:"组团单位",
        			},
        			{
        				name:"tourType.value",
        				label:"散客类型",
        			},
        			{
        				name:"chargeDate",
        				label:"收费日期",
        				format : "date",
        			},
        			{
        				name:"pledgeStatus",
        				label:"状态",
        				format : function(pledgeStatus){
   						 if(pledgeStatus == true){
   							 return "已收费";
   						 }else{
   							 return "待收费";
   						 }
   					 }
        			},
        			{
        				name:"show",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"edit",        				
							text:"收费",	
        					handler:function(index,data,rowEle){
        						
        						if(data.pledgeStatus == true){
									Dialog.alert({
										title : "提示",
										content : "已经收费完成，无法继续收费！"
									});
									return false
									
								}
        						if(data.pledge==null){
        							Dialog.alert({
        								title:"提示",
        								content:"未收押金，不得收费！"
        							});
        							return false
        						}
								       		
        						var remarkForm = new Form({
        							parentNode:".J-dialog-conponent",
        							autoRender:false,
        							model:{
        								defaultButton:false,
        								id:"remarkForm",
        								  items: [{
	        							            name: "groupUnit",
	        							            label: "组团单位",
	        							            readonly:true,
	        							            defaultValue :data.groupUnit
        							            
        								  		},{
        								  			name:"checkInDate",
    												label:"开始日期",
    												type:"date",
    												readonly:true,
    												defaultValue :moment(data.checkInDate).format("YYYY-MM-DD"),
        								  		},{
        								  			name:"checkOutDate",
    												label:"结束日期",
    												type:"date",
    												readonly:true,
    												defaultValue :moment(data.checkOutDate).format("YYYY-MM-DD"),
        								  		},{
    												name:"pledge",
    												label:"应收押金(元)",
    												readonly:true,
    												defaultValue :(data.pledge).toFixed(2),
    											},{
    												name:"pledgeMoney",
    												label:"实收押金(元)",
    												validate : ["required"],
    								                exValidate: function(value) {
    								                	var reg = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
    						                            if (!reg.test(value)) {
    						                                return "请输入正确金额！";
    						                            } else {
    						                                return true;
    						                            }
    						                        }, 
    											},{
    												name:"status",
    												label:"状态",
    												defaultValue:data.status.key,
    												type:"hidden"
    											}]
        								}	       			            	
        						});
    							widget.set("remarkForm",remarkForm);
    							Dialog.showComponent(remarkForm,{
    								defaultButton:false,
    								title: "散客押金确认",
    								setStyle: function(){
										$(".el-dialog-modal .modal").css({
											"top": "10%",
											"width":"50%",
											"left": "25%",
										});
									},
    								buttons :[{
    									id:"save",
    									text:"保存",
     									className:"btn-primary",
    									handler:function(){
    										if(remarkForm.valid()){
    											var FormData = remarkForm.getData();
    											var saveData = {};
    											
    											saveData.servicePoint = widget.get("subnav").getValue("servicePoint");
    											saveData.pkNonMemberGroup = data.pkNonMemberGroup;
    											saveData.pledge = FormData.pledge;
    											saveData.pledgeMoney=FormData.pledgeMoney;
    											saveData.status=FormData.status;
//    											saveData.status=activeUser.pkUser;
    											saveData.pledgeStatus=true;
    											saveData.chargeDate = moment(new Date()).format("YYYY-MM-DD");
    											aw.saveOrUpdate("api/nonmembergroup/savePay",aw.customParam(saveData),function(saveData){
    												Dialog.close();
    							          			widget.get("grid").refresh();
    							       			});
    										};
    									}
    								},{
    									id:"close",
    									text:"取消",
    									handler:function(){
    										Dialog.close();
    									}
    								}]
    							});
        					}
        				},{
        					id:"show",        				
							text:"查看",				
        					handler:function(index,data,rowEle){
        						if(data.pledgeMoney==null){
        							Dialog.alert({
										title : "提示",
										content : "未收费，不能查看！"
									});
									return false
        						}
        						var remarkForm = new Form({
        							parentNode:".J-dialog-conponent",
        							autoRender:false,
        							model:{
        								defaultButton:false,
        								id:"remarkForm",
        								  items: [{
	        							            name: "groupUnit",	        							            
	        							            //validate:"required",
	        							            label: "组团单位",
	        							            readonly:true,
	        							            defaultValue :data.groupUnit
        							            
        								  		},{
        								  			name:"checkInDate",
    												label:"开始日期",
    												type:"date",
    												readonly:true,
    												defaultValue :moment(data.checkInDate).format("YYYY-MM-DD"),
        								  		},{
        								  			name:"checkOutDate",
    												label:"结束日期",
    												type:"date",
    												readonly:true,
    												defaultValue :moment(data.checkOutDate).format("YYYY-MM-DD"),
        								  		},{
    												name:"pledge",
    												label:"应收押金(元)",
    												readonly:true,
    												defaultValue :(data.pledge).toFixed(2),
    												
    											},{
    												name:"pledgeMoney",
    												label:"实收押金(元)",
    												validate : ["required"],
    								                defaultValue:(data.pledgeMoney).toFixed(2), 
    								                readonly:true,
    								                
    											}]
        								}	       			            	
        						});
    							widget.set("remarkForm",remarkForm);
    							Dialog.showComponent(remarkForm,{
    								defaultButton:false,
    								title: "散客订金确认",
    								setStyle: function(){
										$(".el-dialog-modal .modal").css({
											"top": "10%",
											"width":"50%",
											"left": "25%",
										});
									},
    								buttons :[{
    									id:"close",
    									text:"取消",
    									handler:function(){
    										Dialog.close();
    									}
    								}]
    							});
        					}
        				}]

        			}]
        			
        		}
        	});  
        	widget.set("grid",grid);
        }
    };
    module.exports = instance;
});
